<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业Id" prop="companiesId">
        <el-input
          v-model="queryParams.companiesId"
          placeholder="请输入企业Id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="门店面积" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入门店面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月收入" prop="moonIncome">
        <el-input
          v-model="queryParams.moonIncome"
          placeholder="请输入月收入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年收入" prop="yearIncome">
        <el-input
          v-model="queryParams.yearIncome"
          placeholder="请输入年收入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月总成本" prop="moonCost">
        <el-input
          v-model="queryParams.moonCost"
          placeholder="请输入月总成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年总成本" prop="yearCost">
        <el-input
          v-model="queryParams.yearCost"
          placeholder="请输入年总成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月存货成本" prop="moonInventoryCost">
        <el-input
          v-model="queryParams.moonInventoryCost"
          placeholder="请输入月存货成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年存货成本" prop="yearInventoryCost">
        <el-input
          v-model="queryParams.yearInventoryCost"
          placeholder="请输入年存货成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月租金" prop="moonRent">
        <el-input
          v-model="queryParams.moonRent"
          placeholder="请输入月租金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年租金" prop="yearRent">
        <el-input
          v-model="queryParams.yearRent"
          placeholder="请输入年租金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月员工工资" prop="moonSalary">
        <el-input
          v-model="queryParams.moonSalary"
          placeholder="请输入月员工工资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年员工工资" prop="yearSalary">
        <el-input
          v-model="queryParams.yearSalary"
          placeholder="请输入年员工工资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月水电" prop="moonHydropower">
        <el-input
          v-model="queryParams.moonHydropower"
          placeholder="请输入月水电"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年水电" prop="yearHydropower">
        <el-input
          v-model="queryParams.yearHydropower"
          placeholder="请输入年水电"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月软件系统" prop="moonSoftwareSystem">
        <el-input
          v-model="queryParams.moonSoftwareSystem"
          placeholder="请输入月软件系统"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年软件系统" prop="yearSoftwareSystem">
        <el-input
          v-model="queryParams.yearSoftwareSystem"
          placeholder="请输入年软件系统"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月租赁费用" prop="moonLease">
        <el-input
          v-model="queryParams.moonLease"
          placeholder="请输入月租赁费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年租赁费用" prop="yearLease">
        <el-input
          v-model="queryParams.yearLease"
          placeholder="请输入年租赁费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月其他费用" prop="moonOther">
        <el-input
          v-model="queryParams.moonOther"
          placeholder="请输入月其他费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年其他费用" prop="yearOther">
        <el-input
          v-model="queryParams.yearOther"
          placeholder="请输入年其他费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月净利润" prop="moonNetprofit">
        <el-input
          v-model="queryParams.moonNetprofit"
          placeholder="请输入月净利润"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年净利润" prop="yearNetprofit">
        <el-input
          v-model="queryParams.yearNetprofit"
          placeholder="请输入年净利润"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateDate">
        <el-date-picker clearable
          v-model="queryParams.updateDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item> -->
			<el-form-item label="年度" prop="year">
			  <el-input
			    v-model="queryParams.year"
			    placeholder="请输入年度"
			    clearable
			    @keyup.enter.native="handleQuery"
			  />
			</el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['check:cost:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['check:cost:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['check:cost:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['check:cost:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="costList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="storeOperatingCostId" />
      <el-table-column label="企业Id" align="center" prop="companiesId" />
      <!-- <el-table-column label="门店面积" align="center" prop="area" /> -->
      <el-table-column label="月收入" align="center" prop="moonIncome" />
      <el-table-column label="年收入" align="center" prop="yearIncome" />
      <el-table-column label="月总成本" align="center" prop="moonCost" />
      <el-table-column label="年总成本" align="center" prop="yearCost" />
      <el-table-column label="月存货成本" align="center" prop="moonInventoryCost" />
      <el-table-column label="年存货成本" align="center" prop="yearInventoryCost" />
      <el-table-column label="月租金" align="center" prop="moonRent" />
      <el-table-column label="年租金" align="center" prop="yearRent" />
      <el-table-column label="月员工工资" align="center" prop="moonSalary" />
      <el-table-column label="年员工工资" align="center" prop="yearSalary" />
      <el-table-column label="月水电" align="center" prop="moonHydropower" />
      <el-table-column label="年水电" align="center" prop="yearHydropower" />
      <el-table-column label="月软件系统" align="center" prop="moonSoftwareSystem" />
      <el-table-column label="年软件系统" align="center" prop="yearSoftwareSystem" />
      <el-table-column label="月租赁费用" align="center" prop="moonLease" />
      <el-table-column label="年租赁费用" align="center" prop="yearLease" />
      <el-table-column label="月其他费用" align="center" prop="moonOther" />
      <el-table-column label="年其他费用" align="center" prop="yearOther" />
      <el-table-column label="月净利润" align="center" prop="moonNetprofit" />
      <el-table-column label="年净利润" align="center" prop="yearNetprofit" />
      <el-table-column label="年度" align="center" prop="year" />
      <!-- <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="修改时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['check:cost:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['check:cost:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运营成本对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业Id" prop="companiesId">
          <el-input v-model="form.companiesId" placeholder="请输入企业Id" />
        </el-form-item>
        <el-form-item label="门店面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入门店面积" />
        </el-form-item>
        <el-form-item label="月收入" prop="moonIncome">
          <el-input v-model="form.moonIncome" placeholder="请输入月收入" />
        </el-form-item>
        <el-form-item label="年收入" prop="yearIncome">
          <el-input v-model="form.yearIncome" placeholder="请输入年收入" />
        </el-form-item>
        <el-form-item label="月总成本" prop="moonCost">
          <el-input v-model="form.moonCost" placeholder="请输入月总成本" />
        </el-form-item>
        <el-form-item label="年总成本" prop="yearCost">
          <el-input v-model="form.yearCost" placeholder="请输入年总成本" />
        </el-form-item>
        <el-form-item label="月存货成本" prop="moonInventoryCost">
          <el-input v-model="form.moonInventoryCost" placeholder="请输入月存货成本" />
        </el-form-item>
        <el-form-item label="年存货成本" prop="yearInventoryCost">
          <el-input v-model="form.yearInventoryCost" placeholder="请输入年存货成本" />
        </el-form-item>
        <el-form-item label="月租金" prop="moonRent">
          <el-input v-model="form.moonRent" placeholder="请输入月租金" />
        </el-form-item>
        <el-form-item label="年租金" prop="yearRent">
          <el-input v-model="form.yearRent" placeholder="请输入年租金" />
        </el-form-item>
        <el-form-item label="月员工工资" prop="moonSalary">
          <el-input v-model="form.moonSalary" placeholder="请输入月员工工资" />
        </el-form-item>
        <el-form-item label="年员工工资" prop="yearSalary">
          <el-input v-model="form.yearSalary" placeholder="请输入年员工工资" />
        </el-form-item>
        <el-form-item label="月水电" prop="moonHydropower">
          <el-input v-model="form.moonHydropower" placeholder="请输入月水电" />
        </el-form-item>
        <el-form-item label="年水电" prop="yearHydropower">
          <el-input v-model="form.yearHydropower" placeholder="请输入年水电" />
        </el-form-item>
        <el-form-item label="月软件系统" prop="moonSoftwareSystem">
          <el-input v-model="form.moonSoftwareSystem" placeholder="请输入月软件系统" />
        </el-form-item>
        <el-form-item label="年软件系统" prop="yearSoftwareSystem">
          <el-input v-model="form.yearSoftwareSystem" placeholder="请输入年软件系统" />
        </el-form-item>
        <el-form-item label="月租赁费用" prop="moonLease">
          <el-input v-model="form.moonLease" placeholder="请输入月租赁费用" />
        </el-form-item>
        <el-form-item label="年租赁费用" prop="yearLease">
          <el-input v-model="form.yearLease" placeholder="请输入年租赁费用" />
        </el-form-item>
        <el-form-item label="月其他费用" prop="moonOther">
          <el-input v-model="form.moonOther" placeholder="请输入月其他费用" />
        </el-form-item>
        <el-form-item label="年其他费用" prop="yearOther">
          <el-input v-model="form.yearOther" placeholder="请输入年其他费用" />
        </el-form-item>
        <el-form-item label="月净利润" prop="moonNetprofit">
          <el-input v-model="form.moonNetprofit" placeholder="请输入月净利润" />
        </el-form-item>
        <el-form-item label="年净利润" prop="yearNetprofit">
          <el-input v-model="form.yearNetprofit" placeholder="请输入年净利润" />
        </el-form-item>
        <el-form-item label="年度" prop="year">
          <el-input v-model="form.year" placeholder="请输入年度" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="0表示未删除,1表示删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入0表示未删除,1表示删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCost, getCost, delCost, addCost, updateCost } from "@/api/check/cost";

export default {
  name: "Cost",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运营成本表格数据
      costList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companiesId: null,
        area: null,
        moonIncome: null,
        yearIncome: null,
        moonCost: null,
        yearCost: null,
        moonInventoryCost: null,
        yearInventoryCost: null,
        moonRent: null,
        yearRent: null,
        moonSalary: null,
        yearSalary: null,
        moonHydropower: null,
        yearHydropower: null,
        moonSoftwareSystem: null,
        yearSoftwareSystem: null,
        moonLease: null,
        yearLease: null,
        moonOther: null,
        yearOther: null,
        moonNetprofit: null,
        yearNetprofit: null,
        year: null,
        createDate: null,
        updateDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companiesId: [
          { required: true, message: "企业Id不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "门店面积不能为空", trigger: "blur" }
        ],
        moonIncome: [
          { required: true, message: "月收入不能为空", trigger: "blur" }
        ],
        yearIncome: [
          { required: true, message: "年收入不能为空", trigger: "blur" }
        ],
        moonCost: [
          { required: true, message: "月总成本不能为空", trigger: "blur" }
        ],
        yearCost: [
          { required: true, message: "年总成本不能为空", trigger: "blur" }
        ],
        moonInventoryCost: [
          { required: true, message: "月存货成本不能为空", trigger: "blur" }
        ],
        yearInventoryCost: [
          { required: true, message: "年存货成本不能为空", trigger: "blur" }
        ],
        moonRent: [
          { required: true, message: "月租金不能为空", trigger: "blur" }
        ],
        yearRent: [
          { required: true, message: "年租金不能为空", trigger: "blur" }
        ],
        moonSalary: [
          { required: true, message: "月员工工资不能为空", trigger: "blur" }
        ],
        yearSalary: [
          { required: true, message: "年员工工资不能为空", trigger: "blur" }
        ],
        moonHydropower: [
          { required: true, message: "月水电不能为空", trigger: "blur" }
        ],
        yearHydropower: [
          { required: true, message: "年水电不能为空", trigger: "blur" }
        ],
        moonSoftwareSystem: [
          { required: true, message: "月软件系统不能为空", trigger: "blur" }
        ],
        yearSoftwareSystem: [
          { required: true, message: "年软件系统不能为空", trigger: "blur" }
        ],
        moonLease: [
          { required: true, message: "月租赁费用不能为空", trigger: "blur" }
        ],
        yearLease: [
          { required: true, message: "年租赁费用不能为空", trigger: "blur" }
        ],
        moonOther: [
          { required: true, message: "月其他费用不能为空", trigger: "blur" }
        ],
        yearOther: [
          { required: true, message: "年其他费用不能为空", trigger: "blur" }
        ],
        moonNetprofit: [
          { required: true, message: "月净利润不能为空", trigger: "blur" }
        ],
        yearNetprofit: [
          { required: true, message: "年净利润不能为空", trigger: "blur" }
        ],
        year: [
          { required: true, message: "年度不能为空", trigger: "blur" }
        ],
        createDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询运营成本列表 */
    getList() {
      this.loading = true;
      listCost(this.queryParams).then(response => {
        this.costList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeOperatingCostId: null,
        companiesId: null,
        area: null,
        moonIncome: null,
        yearIncome: null,
        moonCost: null,
        yearCost: null,
        moonInventoryCost: null,
        yearInventoryCost: null,
        moonRent: null,
        yearRent: null,
        moonSalary: null,
        yearSalary: null,
        moonHydropower: null,
        yearHydropower: null,
        moonSoftwareSystem: null,
        yearSoftwareSystem: null,
        moonLease: null,
        yearLease: null,
        moonOther: null,
        yearOther: null,
        moonNetprofit: null,
        yearNetprofit: null,
        year: null,
        createBy: null,
        createDate: null,
        updateBy: null,
        updateDate: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeOperatingCostId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运营成本";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storeOperatingCostId = row.storeOperatingCostId || this.ids
      getCost(storeOperatingCostId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运营成本";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.storeOperatingCostId != null) {
            updateCost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeOperatingCostIds = row.storeOperatingCostId || this.ids;
      this.$modal.confirm('是否确认删除运营成本编号为"' + storeOperatingCostIds + '"的数据项？').then(function() {
        return delCost(storeOperatingCostIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/cost/export', {
        ...this.queryParams
      }, `cost_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
